﻿using CaseManagerLibrary.Log;
using PSDLib.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PSDLib.Download
{
    public interface IDownloadServer
    {
        string ID { get; set; }
        string Name { get; set; }
        ILog Logger { get; set; }
        string ServerIP { get; set; }
        int ServerPort { get; set; }
        string ServerUser { get; set; }
        string ServerPassword { get; set; }
        bool Online { get; set; }
        bool Init();
        void Cleanup();
        bool Login();
        bool ReLogin();
        void Logout();
        IDownloader CreateDownload(dl_files file);
        void ResetSessionInfo(IDownloader downloader);
        string GetLastError();
        List<ChannelInfo> GetChannelList();

        event EventHandler OnDisConnect;
        event EventHandler OnReConnect;
    }

    public static class DownloadServerFactory
    {
        public static IDownloadServer Create(string type)
        {
            IDownloadServer server = null;

            try
            {
                if (type == DownloadServerType.Camera.ToString())
                {
                    type = DownloadServerType.DaHua.ToString();
                }

                string typeName = "PSDLib.Download." + type + "DownloadServer";
                System.Reflection.Assembly assm = System.Reflection.Assembly.Load("PSDLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");
                var dsType = assm.GetType(typeName);

                server = (IDownloadServer)Activator.CreateInstance(dsType);
            }
            catch (Exception ex)
            { }

            return server;
        }

        public static IDownloadServer Create(dl_server_config server)
        {
            IDownloadServer s = Create(server.storage_server_type);

            if (s != null)
            {
                s.ID = server.id;
                s.ServerPassword = server.storage_server_password;
                s.ServerUser = server.storage_server_user;
                s.ServerPort = server.storage_server_port;
                s.ServerIP = server.storage_server_ip;
            }

            return s;
        }
    }


   

    public enum DownloadServerType
    {
        HK,
        DaHua,
        DH8900,
        Camera,
    }
}
